<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8" />
    <title>智慧社区-物业管理</title>

    <meta name="description" content="Dashboard" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/resource/assets/img/favicon.png" type="image/x-icon">

    <!--Basic Styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/bootstrap.min.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/font-awesome.min.css" rel="stylesheet" />

    <!--Beyond styles-->
    <link href="${pageContext.request.contextPath}/resource/assets/css/beyond.min.css" rel="stylesheet" type="text/css" />
    <link href="${pageContext.request.contextPath}/resource/assets/css/animate.min.css" rel="stylesheet" />
<script src="${pageContext.request.contextPath}/resource/assets/js/skins.min.js"></script>
</head>
<body>
<%@include file="../../header.jsp" %>
	<!-- Main Container -->
    <div class="main-container container-fluid">
        <!-- Page Container -->
        <div class="page-container">
            <!-- Page Sidebar -->
            <%@include file="../nav.jsp" %><!-- /Page Sidebar -->
            <!-- Page Content -->
            <div class="page-content">
                <!-- Page Breadcrumb -->
                <div class="page-breadcrumbs">
                    <ul class="breadcrumb" style="margin-top: 8px;">
                        <li>
                            <i class="fa fa-home"></i>
                            <a href="#">首页</a>
                        </li>
                        <li><a href="#">系统设置</a></li>
                        <li class="active">单元机设置</li>
                    </ul>
                </div><!-- /Page Breadcrumb -->
                <!-- Page Header -->
                <div class="page-header position-relative">
                    <div class="header-title" style="margin-top: 2px;">
                        <h1>
                            单元机设置
                        </h1>
                    </div><!--Header Buttons-->
                    <div class="header-buttons">
                        <a class="sidebar-toggler" href="#">
                            <i class="fa fa-arrows-h"></i>
                        </a>
                        <a class="refresh" id="refresh-toggler" href="">
                            <i class="glyphicon glyphicon-refresh"></i>
                        </a>
                        <a class="fullscreen" id="fullscreen-toggler" href="#">
                            <i class="glyphicon glyphicon-fullscreen"></i>
                        </a>
                    </div><!--Header Buttons End-->
                </div><!-- /Page Header -->
                <!-- Page Body -->
                <div class="page-body">
                	<div class="row">
	                	<div class="col-lg-6 col-sm-6 col-xs-12">
	                		<div class="widget">
                                        <div class="widget-header bordered-bottom bordered-palegreen">
                                            <span class="widget-caption">门禁设置</span>
                                        </div>
                                        <div class="widget-body">
                                           <div id="registration-form">
                                                        <form role="form">
                                                            <div class="form-title">
                                                            	门禁设置
                                                            </div>
                                                            	
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="reportTime" type="text" class="form-control"  placeholder="心跳上报周期（单位：秒）">
                                                                    <i class="glyphicon glyphicon-align-right"></i>
                                                                </span>
                                                            </div>
                                                            
                                                          <div class="form-group">
                                                                        <span class="input-icon icon-right">
                                                                            <input id="callTime" type="text" class="form-control" placeholder="呼叫通话时间（单位：秒）">
                                                                            <i class="glyphicon glyphicon-sort-by-order"></i>
                                                                        </span>
                                                                    </div>
                                                                    
                                                            
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="appServerIp" type="text" class="form-control" placeholder="应用服务器地址（IP）">
                                                                    <i class="glyphicon glyphicon-map-marker"></i>
                                                                </span>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="appServerPort" type="text" class="form-control" placeholder="应用服务器端口">
                                                                    <i class="glyphicon glyphicon-map-marker"></i>
                                                                </span>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="videoServerIp" type="text" class="form-control" placeholder="视频服务器地址（IP）">
                                                                    <i class="glyphicon glyphicon-map-marker"></i>
                                                                </span>
                                                            </div>
                                                            <div class="form-group">
                                                                <span class="input-icon icon-right">
                                                                    <input id="videoServerPort" type="text" class="form-control" placeholder="视频服务器端口">
                                                                    <i class="glyphicon glyphicon-map-marker"></i>
                                                                </span>
                                                            </div>
                                                            
                                                            <hr class="wide" />
                                                            <div class="row" style="text-align: right;margin-right: 10px;">
                                                            	<button type="button" class="btn btn-warning" onclick="save()"> 下发配置 </button>
                                                            	<button type="button" class="btn btn-blue" onclick="resetAll()"> 重置单元机 </button>
                                                            	<button type="button" class="btn btn-danger" onclick="restart()"> 重启单元机 </button>
                                                            </div>
                                                            
                                                        </form>
                                                    </div> 
                                        </div>
                                    </div>
	                	</div>
                	
                    </div>
                </div><!-- /Page Body -->
             </div><!-- /Page Content -->
     	</div><!-- /Page Container -->
     </div><!-- /Main Container -->
     	<script src="${pageContext.request.contextPath}/resource/assets/js/jquery-2.0.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/resource/assets/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath}/resource/assets/js/select2/select2.js"></script>
    <!--Beyond Scripts-->
    <script src="${pageContext.request.contextPath}/resource/assets/js/beyond.min.js"></script>
    <script type="text/javascript">
    	$('.loading-container').hide();
    	$('#menu_settings').addClass("active open");
    	$('#menu_um_setting').addClass("active");
    	
    	function save(){
    		if (confirm("警告：此操作将更改所有单元机的配置，某些配置错误将导致单元机无法工作，请确认是否继续？")) {
    			var patrn = /^(?:[1-9]\d*|0)$/;
        		var re =  /^([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.([0-9]|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$/; 
        		
        		var callTime = $('#callTime').val();
        		
        		if($.trim(callTime) != ""){
        			var result = patrn.test(callTime);	
        			if(!result){
        				alert("呼叫通话时间必须是正整数");
        				return;
        			}
        		}
        		
        		var reportTime = $('#reportTime').val();
        		
        		if($.trim(reportTime) != ""){
        			var result = patrn.test(reportTime);	
        			if(!result){
        				alert("心跳上报时间必须是正整数");
        				return;
        			}
        		}
        		
        		var appServerIp = $('#appServerIp').val();
        		
        		if($.trim(appServerIp) != ""){
        			var result = re.test(appServerIp);
        			if(!result){
        				alert("应用服务器地址必须是有效的IP地址");
        				return;
        			}
        		}
        		
        		var appServerPort = $('#appServerPort').val();
        		
        		if($.trim(appServerPort) != ""){
        			var result = patrn.test(appServerPort);
        			if(!result){
        				alert("应用服务器端口号必须是正整数");
        				return;
        			}
        		}
        		
        		var videoServerIp = $('#videoServerIp').val();
        		
        		if($.trim(videoServerIp) != ""){
        			var result = re.test(videoServerIp);
        			if(!result){
        				alert("视频服务器地址必须是有效的IP地址");
        				return;
        			}
        		}
        		
        		var videoServerPort = $('#videoServerPort').val();
        		
        		if($.trim(videoServerPort) != ""){
        			var result = patrn.test(videoServerPort);
        			if(!result){
        				alert("视频服务器端口号必须是正整数");
        				return;
        			}
        		}
        		
        		if($.trim(callTime) == "" && $.trim(reportTime) == "" && $.trim(appServerIp) == "" && $.trim(appServerPort) == "" && $.trim(videoServerIp) == "" && $.trim(videoServerPort) == ""){
        			alert("请至少填写一项配置");
        			return;
        		}
        		
        		$('.loading-container').show();
        		$.ajax({
        			"url":"${pageContext.request.contextPath}/umconfig_save",
        			"data":{callTime:callTime,reportTime:reportTime,appServerIp:appServerIp,appServerPort:appServerPort,videoServerIp:videoServerIp,videoServerPort:videoServerPort},
        			"type":'post',
        			"dataType":'json',
        			"success" : function(result) {
        				$('.loading-container').hide();
        				
        				if (result == 201){
        					alert("鉴权已过期，请重新登录");
        					location.href="/WisdomHome";
        					return;
        				}
        				
        				if (result == 200) {
        					alert("配置下发成功，单元机将在重启后恢复工作");
        					$('#callTime').val("");
        					$('#reportTime').val("");
        					$('#appServerIp').val("");
        					$('#appServerPort').val("");
        					$('#videoServerIp').val("");
        					$('#videoServerPort').val("");
        				}else {
        					alert("提交失败，请检查网络后重试~");
        				}
        			},
        			"error" : function(msg) {
        				alert("error msg:"+msg.responseText);
        			}
        		});
			}
    	}
    	
    	function resetAll(){
        	if(confirm("警告：此操作将使所有的单元门禁恢复默认设置，确认是否继续？")){
        		$.ajax({
            		url:"${pageContext.request.contextPath}/umconfig_reset",
            		type:"post",
            		success:function(result){
            			if (result == 201) {
        					alert("鉴权已过期，请重新登录");
        					location.href="/SmartComm";
        		   			return;
        				}
            			
            			if(result == 200){
            				alert("配置成功，所有的单元机将在恢复默认配置后继续工作");
            			}
            		},
            		error:function(msg){
            			
            		}
            	});
        	}
        }
        
        function restart(){
        	if(confirm("警告：此操作将使所有的单元门禁重启，确认是否继续？")){
        		$.ajax({
            		url:"${pageContext.request.contextPath}/umconfig_restart",
            		type:"post",
            		success:function(result){
            			if (result == 201) {
        					alert("鉴权已过期，请重新登录");
        					location.href="/WisdomHome";
        		   			return;
        				}
            			
            			if(result == 200){
            				alert("配置成功，所有的单元机将在重新启动后继续工作");
            			}
            		},
            		error:function(msg){
            			
            		}
            	});
        	}
        }
    </script>
</body>
</html>